﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class appleader_weight_sort : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["projectid"] == null)
        {
            MasApp.BLL.ResponseHelper.alert("非法链接", "../projects/addmission.aspx");
        }
        else
        {
            //DataTable dt = app.BLL.Search.SearchUsersIDNameName(int.Parse(Session["organizationid"].ToString()));
            int projectid = Convert.ToInt32(Request["projectid"].ToString());
            foreach (GridViewRow gr in GridView1.Rows)
            {
                Label lb_username = (Label)gr.Cells[1].FindControl("Label2");
                if (MasApp.BLL.Search.SearchHaveUsernameInWeightmission(lb_username.Text, projectid) ==1)
                {
                    CheckBox cb = (CheckBox)gr.Cells[3].FindControl("CheckBox1");
                    cb.Checked = true;
                }

            }
        }
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        int projectid = Convert.ToInt32(Request["projectid"].ToString());

        MasApp.Model.userrole ur = new MasApp.Model.userrole();
        ur.Userroleid = 8;

        MasApp.Model.weightmission wm = new MasApp.Model.weightmission();
        wm.Projectid = projectid;
        wm.State = "未做的";

        foreach (GridViewRow gr in GridView1.Rows)
        {
            CheckBox cb = (CheckBox)gr.Cells[3].FindControl("CheckBox1");
            if (cb.Checked == true)
            {
                Label lb_username = (Label)gr.Cells[1].FindControl("Label2");
                if (MasApp.BLL.Search.SearchHaveUsernameInWeightmission(lb_username.Text, projectid) == 0)
                {
                    Label lb_userid = (Label)gr.Cells[0].FindControl("Label1");
                    ur.Userid = int.Parse(lb_userid.Text);
                    wm.Actor = lb_username.Text;

                    //wm.Weightmissionid = app.BLL.reg.AddWeightExpert(wm, ur);
                    wm.Weightmissionid = MasApp.BLL.reg.RegWeightMission(wm);
                    MasApp.BLL.ResponseHelper.alert("您已经选择用户为当前项目的评权专家（）"
                        , "sort.aspx?projectid=" + projectid.ToString());

                }
            }
            else
            {
                Label lb_username = (Label)gr.Cells[1].FindControl("Label2");
                if (MasApp.BLL.Search.SearchHaveUsernameInWeightmission(lb_username.Text, projectid) > 0)
                {
                    wm.Actor = lb_username.Text;
                    if (!MasApp.BLL.Delete.DeleteWeightMission(wm))
                    {
                        Label lb = new Label();
                        lb.Text = "<img src='../../images/X.png' alt='' />用户【" 
                            + lb_username.Text + "】已经接受任务，不能删除！<br />";
                        Panel1.Controls.Add(lb);
                    }
                    else
                    {
                        MasApp.BLL.ResponseHelper.alert("您已经选择用户为当前项目的评级专家（评估员）"
                            , "sort.aspx?projectid=" + projectid.ToString());
                    }
                }
            }
        }
    }
}
